package com.orangecy.rulesubscribe.config;

import com.google.common.collect.Lists;
import com.orangecy.common.enums.StatusEnum;
import com.orangecy.rulesubscribe.define.RuleDefineDTO;
import com.orangecy.rulesubscribe.rulebuilder.ItemCenterRuleBuilder;
import com.orangecy.rulesubscribe.rulebuilder.MarketingRuleBuilder;
import org.apache.commons.collections.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Author : orangeCy.奕
 * @Product : 猎豹养成产品源码
 * @Time : 2021/10/22
 * @Desc : 规则定义池配置
 * <p>
 *     [
 *          {
 *              "ruleCode":"r_code1",
 *              "ruleName":"r_name1",
 *              "ruleDimension":2
 *              "ruleDesc":"r_desc1",
 *              "actuatorType":1,
 *              "actuatorProcessor":"xxProcessor1",
 *              "actuatorContext":"xxxJson"
 *              "status":1
 *          }
 *     ]
 * </p>
 */
public class RuleDefinePoolConfig {

    private static List<RuleDefineDTO> initRuleDefinePool() {
        List<RuleDefineDTO> ruleDefinePool = Lists.newArrayList();
        ruleDefinePool.add(ItemCenterRuleBuilder.buildIcRule4SkuOverSize());
        ruleDefinePool.add(MarketingRuleBuilder.buildMkRule4PriceOverSize());
        return ruleDefinePool;
    }

    /**
     * 规则池中查找匹配ruleCodes规则
     * @param ruleCodes
     * @return
     */
    public static List<RuleDefineDTO> findRulesByCodes(List<String> ruleCodes) {
        List<RuleDefineDTO> ruleDefineDTOS = initRuleDefinePool();
        if(CollectionUtils.isEmpty(ruleCodes) || CollectionUtils.isEmpty(ruleDefineDTOS)) {
            return Collections.emptyList();
        }
        //组件code和DTO
        Map<String, RuleDefineDTO> codeAndRuleMap = ruleDefineDTOS.stream().filter(e -> Objects.equals(e.getStatus(), StatusEnum.EFFECT.getCode()))
                .collect(Collectors.toMap(RuleDefineDTO::getRuleCode, e -> e, (k1, k2) -> k1));

        return ruleCodes.stream().filter(codeAndRuleMap::containsKey).map(codeAndRuleMap::get)
                .filter(Objects::nonNull).collect(Collectors.toList());
    }
}